<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品规格" prop="commercialSog">
      <el-input v-model="dataForm.commercialSog" placeholder="商品规格"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="shopOriginalCost">
      <el-input v-model="dataForm.shopOriginalCost" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="商品低价" prop="shopCommodityPrice">
      <el-input v-model="dataForm.shopCommodityPrice" placeholder="商品低价"></el-input>
    </el-form-item>
    <el-form-item label="商品砍价规则" prop="shopBargainingRules">
      <el-input v-model="dataForm.shopBargainingRules" placeholder="商品砍价规则"></el-input>
    </el-form-item>
    <el-form-item label="活动分享描述" prop="activitiesText">
      <el-input v-model="dataForm.activitiesText" placeholder="活动分享描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          goodsId: '',
          commercialSog: '',
          startTime: '',
          endTime: '',
          shopOriginalCost: '',
          shopCommodityPrice: '',
          shopBargainingRules: '',
          activitiesText: ''
        },
        dataRule: {
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          commercialSog: [
            { required: true, message: '商品规格不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          shopOriginalCost: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          shopCommodityPrice: [
            { required: true, message: '商品低价不能为空', trigger: 'blur' }
          ],
          shopBargainingRules: [
            { required: true, message: '商品砍价规则不能为空', trigger: 'blur' }
          ],
          activitiesText: [
            { required: true, message: '活动分享描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/content/bargainactivity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsId = data.bargainActivity.goodsId
                this.dataForm.commercialSog = data.bargainActivity.commercialSog
                this.dataForm.startTime = data.bargainActivity.startTime
                this.dataForm.endTime = data.bargainActivity.endTime
                this.dataForm.shopOriginalCost = data.bargainActivity.shopOriginalCost
                this.dataForm.shopCommodityPrice = data.bargainActivity.shopCommodityPrice
                this.dataForm.shopBargainingRules = data.bargainActivity.shopBargainingRules
                this.dataForm.activitiesText = data.bargainActivity.activitiesText
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/content/bargainactivity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'goodsId': this.dataForm.goodsId,
                'commercialSog': this.dataForm.commercialSog,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'shopOriginalCost': this.dataForm.shopOriginalCost,
                'shopCommodityPrice': this.dataForm.shopCommodityPrice,
                'shopBargainingRules': this.dataForm.shopBargainingRules,
                'activitiesText': this.dataForm.activitiesText
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
